package com.wanyang.controller;

import com.wanyang.service.AuthService;
import com.wanyang.vo.ApiResult;
import com.wanyang.vo.AuthVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "授权相关api")
@RestController
@RequestMapping("auth")
public class AuthController {
    @Autowired
    private AuthService authService;

    @Data
    public static class LoginDTO {
        @ApiModelProperty(value = "手机号", required = true)
        private String mobile;
        @ApiModelProperty(value = "验证码", required = true)
        private String smsCode;
    }

    @Data
    public static class RegisterDTO {
        @ApiModelProperty(value = "手机号", required = true)
        private String mobile;
        @ApiModelProperty(value = "验证码", required = true)
        private String smsCode;
        @ApiModelProperty(value = "邀请码", required = false)
        private String shareCode;
    }

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public ApiResult login(@RequestBody LoginDTO loginDTO) {

        return ApiResult.success(authService.login(loginDTO.mobile, loginDTO.smsCode), null);
    }

    @ApiOperation(value = "注册")
    @PostMapping("register")
    public ApiResult register(@RequestBody RegisterDTO registerDTO) {
        AuthVO authVO = authService.register(registerDTO.mobile, registerDTO.smsCode, registerDTO.shareCode);
        return ApiResult.success(authVO, null);
    }

    @ApiOperation(value = "refreshToken", notes = "调用该接口时，header不要带Token！！！")
    @GetMapping("refreshToken")
    public ApiResult refreshToken(@RequestParam String refreshToken) {
        AuthVO authVO = authService.refresh(refreshToken);
        return ApiResult.success(authVO, null);
    }

}
